$(document).ready(function() {
	
	$("#selectall").click(function() {
        if ($("#selectall").prop("checked") == true) {
            $(".chek_all").prop("checked", true);
        } else {
            $(".chek_all").prop("checked", false);
        }
    });
	
	$("#clnt_company").select2({
       placeholder: "Select Company",
	   width: '100%'
    });
	
	$("#clnt_type").select2({
       placeholder: "Select Client type",
	   width: '100%'
    });
	
	$("#clnt_source").select2({
       placeholder: "Select Client Source",
	   width: '100%'
    });
	
	$(".form_datetime").datepicker({
		format: 'yyyy-mm-dd'
	});
	
	/*$("#comp_industry").select2({
       placeholder: "Select Company Industry",
	   width: '100%'
    });
	
	$("#comp_type").select2({
       placeholder: "Select Company Type",
	   width: '100%'
    });*/
	
	// Company Popup
	$("#add_company").click(function() {
        var chkArray = [];
        $(".chek_all:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',');
        //$("#ac_property_id").val(selected);
        $("#myModal_company").modal('show');

    });
	
	$("#add_btn_company").click(function() {
		
        var comp_name = $("#comp_name").val();
		var comp_type = $("#comp_type").val();
		var comp_industry = $("#comp_industry").val();
		var comp_email = $("#comp_email").val();
		var comp_phone = $("#comp_phone").val();
		var comp_adrline1 = $("#comp_adrline1").val();
        $("#error_add_client").hide();
		
		if (comp_name == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter name');
            $("#comp_name").css('border-color', 'red');
            return false;
        } else if (comp_type == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please select type');
            $("#comp_type").css('border-color', 'red');
            return false;
        } else if (comp_industry == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please select industry');
            $("#comp_industry").css('border-color', 'red');
            return false;
        } else if (comp_email == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter email');
            $("#comp_email").css('border-color', 'red');
            return false;
        } else {
            var postdata = {};
            var obj = $(this);
            postdata['comp_name'] = comp_name;
			postdata['comp_type'] = comp_type;
			postdata['comp_industry'] = comp_industry;
			postdata['comp_email'] = comp_email;
			postdata['comp_phone'] = comp_phone;
			postdata['comp_adrline1'] = comp_adrline1;
			
            jQuery.post(ADMIN_URL + 'client/addcompany', postdata, function(company_id) {
            
				$("#success_add_client").show();
				window.setTimeout(function() {
					 $(".chek_all").prop("checked", false);
					 $("#success_add_client").hide();
					 $("#myModal_company").modal('hide');
				}, 3000);
				
				var postdata = {};
				postdata['company_last_id'] = company_id;
				jQuery.post(ADMIN_URL + 'client/getcompanylist', postdata, function(result) {
				   $('#clnt_company').html(result);
				   $("#clnt_company").select2("val", company_id);
				});
			});
        }
    });
});